import 'package:flutter/material.dart';
import './PPConditionChoiceItem.dart';

class PPConditionKindChoiceView extends StatefulWidget {
    PPConditionKindChoiceView({Key key, this.title, this.type, this.data, this.action})
        : super(key: key);

    final String title;
    final List<Map> data;
    final Function action;
    final type;

    _PPConditionKindChoiceState createState() => _PPConditionKindChoiceState();
}

/// **** 如果需要自动换行可以使用组件 Wrap ****/ 
class _PPConditionKindChoiceState extends State<PPConditionKindChoiceView> {

    String title;
    List<Map> data;

    @override
    void initState() {
        title = widget.title;
        data = widget.data;
        super.initState();
    }

    @override
    Widget build(BuildContext context) {
        double screenWidth = MediaQuery.of(context).size.width;
        double itemWidth = (screenWidth - 90)/3.0;
        return (Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            Container(
                height: 14,
                margin: EdgeInsets.fromLTRB(0, 25, 0, 0),
                padding: EdgeInsets.fromLTRB(5, 0, 0, 0),
                decoration: BoxDecoration(
                    color: Colors.white10,
                    border: Border(
                        left: BorderSide(
                            color: Color.fromRGBO(63, 227, 206, 1),
                            width: 3,
                            style: BorderStyle.solid)
                        )
                    ),
                child:
                    Text(title, style: TextStyle(fontSize: 12, color: Colors.black)),
            ),
            Container(
                margin: EdgeInsets.all(0),
                padding: EdgeInsets.fromLTRB(30, 15, 0, 0),
                child: Wrap(
                    spacing: 15.0,
                    children: _getRowChildren(data, itemWidth))
            ),
        ]));
    }

    _getRowChildren(List<Map> items, double itemWidth) {
        return(items.map((item){
            int index = data.indexOf(item);
            return (PPConditionChoiceItem(data: item, itemWidth: itemWidth, index: index, action: _onPressButtonActon));
        }).toList());
    }

    _onPressButtonActon(item, index) {
        if(widget.action != null) {
			widget.action(widget.type, data, item, index);
		}
    }
}
